package by.bsuir.csschat.ui;
import by.bsuir.csschat.privconv.PrivateConversation; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseWheelEvent; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import javax.swing.JTextField;
import java.awt.Rectangle;

import javax.swing.JScrollPane;


public class frmPrivateConv extends javax.swing.JFrame {
    private PrivateConversation _refConv;

    public frmPrivateConv(PrivateConversation iRef) {
        _refConv=iRef;
        initComponents();
        setSize(200,200);
        setVisible(true);
        requestFocus();
        setTitle(_refConv.GetTo().GetName());
        typeBox.requestFocus();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeBox = new javax.swing.JTextField();
        jScroller = new javax.swing.JScrollPane();
        msgArea = new javax.swing.JTextArea();

        setTitle("Private Conversation");
        setName("frmPrivateConv"); // NOI18N

        typeBox.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        typeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeBoxActionPerformed(evt);
            }
        });
        typeBox.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                typeBoxKeyTyped(evt);
            }
        });
        getContentPane().add(typeBox, java.awt.BorderLayout.SOUTH);

        msgArea.setBackground(new java.awt.Color(244, 241, 241));
        msgArea.setColumns(20);
        msgArea.setEditable(false);
        msgArea.setFont(new java.awt.Font("Times New Roman", 0, 10)); // NOI18N
        msgArea.setRows(5);
        msgArea.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                msgAreaMouseWheelMoved(evt);
            }
        });
        jScroller.setViewportView(msgArea);

        getContentPane().add(jScroller, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void typeBoxKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_typeBoxKeyTyped
        if(evt.getKeyChar()=='\n')    
            SendMsg();
    }//GEN-LAST:event_typeBoxKeyTyped

    private void msgAreaMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_msgAreaMouseWheelMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_msgAreaMouseWheelMoved

    private void typeBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_typeBoxActionPerformed
    
    
    public void SendMsg(){
        if(typeBox.getText().equals("/clear") || typeBox.getText().equals("/cls") ){
            ClearRecvArea();
        }else
            _refConv.SendMessage(typeBox.getText());
        typeBox.setText("");
    }
    
    public void AddRecvLine(String iMsg){
        msgArea.setText(msgArea.getText()+iMsg+"\n");
        msgArea.scrollRectToVisible(new Rectangle(0,msgArea.getHeight()-20,1,1));
    }
    public void ClearRecvArea(){
        msgArea.setText("");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScroller;
    private javax.swing.JTextArea msgArea;
    private javax.swing.JTextField typeBox;
    // End of variables declaration//GEN-END:variables
    
}

